<template>
  <el-container class="home-container">
    <el-header
      ><div>
        <img src="../assets/heima.png" alt="" /><span>电商后台管理系统</span>
      </div>

      <el-button type="info" @click="clean">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="!coll ? '200px' : '64px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409Eff"
          :unique-opened="true"
          :collapse="coll"
          :collapse-transition="false"
          router
          :default-active="indexActive"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveIndex('/' + subItem.path)"
            >
              <!-- 二级菜单模板 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-s-grid"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main> <router-view></router-view></el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      indexActive: window.sessionStorage.getItem('index'),
      coll: false,
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 左侧菜单数据
      menuList: null
    }
  },
  created() {
    this.getMeunList()
  },
  methods: {
    saveIndex(data) {
      sessionStorage.setItem('index', data)
      this.indexActive = window.sessionStorage.getItem('index')
    },
    toggleCollapse() {
      this.coll = !this.coll
    },
    clean() {
      window.sessionStorage.setItem('token', '')
      this.$router.push('login')
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath)
    },
    async getMeunList() {
      const { data } = await this.$http.get('menus')

      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      this.menuList = data.data

      // console.log(this)
      // console.log(this.$router)
      // console.log(this.$route)
    }
  }
}
</script>
<style lang="less" scoped>
.toggle-button {
  background-color: #4a5064;
  color: #fff;
  font-size: 10px;
  text-align: center;
  letter-spacing: 2.2px;
  line-height: 16px;
  cursor: pointer;
}
.iconfont {
  margin-right: 10px;
}
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
